package net.tarine.gastronomi.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;

import net.tarine.gastronomi.server.model.ForumMessage;
import net.tarine.gastronomi.server.model.ForumThread;
import net.tarine.gastronomi.server.model.Gas;
import net.tarine.gastronomi.server.model.GasUser;
import net.tarine.gastronomi.shared.to.ForumMessageTo;
import net.tarine.gastronomi.shared.to.ForumThreadTo;
import net.tarine.gastronomi.shared.to.GasTo;
import net.tarine.gastronomi.shared.to.GasUserTo;

import com.google.appengine.api.datastore.Text;

public class ToConverter {

	//Gas
	public static GasTo convert(PersistenceManager pm, Gas entity) {
		GasTo to = new GasTo();
		to.setEmailAddress(entity.getEmailAddress());
		//intentionally commented to.setEmailPassword(entity.getEmailPassword());
		to.setEmailSmtp(entity.getEmailSmtp());
		to.setId(entity.getId());
		to.setName(entity.getName());
		return to;
	}
	public static Gas convert(PersistenceManager pm, GasTo to) {
		Gas entity = null;
		if (to.getId() != null) entity = pm.getObjectById(Gas.class, to.getId());
		if (entity == null) entity = new Gas();
		entity.setEmailAddress(to.getEmailAddress());
		entity.setEmailPassword(to.getEmailPassword());
		entity.setEmailSmtp(to.getEmailSmtp());
		entity.setId(to.getId());
		entity.setName(to.getName());
		return entity;
	}
	
	//GasUser
	public static GasUserTo convert(PersistenceManager pm, GasUser entity) {
		if (entity == null) return null;
		GasUserTo to = new GasUserTo();
		//References
		if (entity.getUser() != null) to.setAuthUserId(entity.getUser().getUserId());
		if (entity.getUser() != null) to.setFederatedIdentity(entity.getUser().getFederatedIdentity());
		//Properties
		to.setId(entity.getId());
		to.setGasId(entity.getGasId());
		to.setEmail(entity.getEmail());
		to.setNickname(entity.getNickname());
		to.setPhoneNumber(entity.getPhoneNumber());
		to.setReceiveForumEmail(entity.getReceiveForumEmail());
		to.setReceiveOrderEmail(entity.getReceiveOrderEmail());
		to.setRoleId(entity.getRoleId());
		return to;
	}
	public static GasUser convert(PersistenceManager pm, GasUserTo to) {
		if (to == null) return null;
		GasUser entity = null;
		//References
		if (to.getId() != null) entity = pm.getObjectById(GasUser.class, to.getId());
		if (entity == null) entity = new GasUser();
		//Properties
		entity.setGasId(to.getGasId());
		entity.setEmail(to.getEmail());
		entity.setNickname(to.getNickname());
		entity.setPhoneNumber(to.getPhoneNumber());
		entity.setReceiveForumEmail(to.getReceiveForumEmail());
		entity.setReceiveOrderEmail(to.getReceiveOrderEmail());
		entity.setRoleId(to.getRoleId());
		return entity;
	}
	
	//ForumMessage
	public static ForumMessageTo convert(PersistenceManager pm, ForumMessage entity, ForumThreadTo threadTo) {
		if (entity == null) return null;
		ForumMessageTo to = new ForumMessageTo();
		//References
		to.setForumThreadTo(threadTo);
		GasUser user = pm.getObjectById(GasUser.class, entity.getGasUserId());
		to.setGasUserTo(convert(pm, user));
		//Properties
		to.setId(entity.getId());
		to.setCreationDt(entity.getCreationDt());
		to.setModifiedDt(entity.getModifiedDt());
		to.setText(entity.getText().getValue());
		return to;
	}
	public static ForumMessage convert(PersistenceManager pm, ForumMessageTo to, ForumThread thread) {
		if (to == null) return null;
		ForumMessage entity = null;
		//References
		if (to.getId() != null) entity = pm.getObjectById(ForumMessage.class, to.getId());
		if (entity == null) entity = new ForumMessage();
		entity.setForumThread(thread);
		if (to.getGasUserTo() != null) entity.setGasUserId(to.getGasUserTo().getId());
		//Properties
		entity.setCreationDt(to.getCreationDt());
		entity.setModifiedDt(to.getModifiedDt());
		entity.setText(new Text(to.getText()));
		return entity;
	}
	
	//ForumThread
	public static ForumThreadTo convert(PersistenceManager pm, ForumThread entity) {
		if (entity == null) return null;
		//References
		ForumThreadTo to = new ForumThreadTo();
		List<ForumMessageTo> forumMessageToList = new ArrayList<ForumMessageTo>();
		for (ForumMessage fm:entity.getForumMessageList()) {
			forumMessageToList.add(convert(pm, fm, to));
		}
		to.setForumMessagesToList(forumMessageToList);
		GasUser user = pm.getObjectById(GasUser.class, entity.getGasUserId());
		to.setGasUserTo(convert(pm, user));
		if (entity.getGas() != null) to.setGasId(entity.getGas().getId());
		//Properties
		to.setId(entity.getId());
		to.setCreationDt(entity.getCreationDt());
		to.setLastMessageDt(entity.getLastMessageDt());
		to.setModifiedDt(entity.getModifiedDt());
		to.setTitle(entity.getTitle());
		return to;
	}
	public static ForumThread convert(PersistenceManager pm, ForumThreadTo to) {
		if (to == null) return null;
		//References
		ForumThread entity = null;
		if (to.getId() != null) entity = pm.getObjectById(ForumThread.class, to.getId());
		if (entity == null) entity = new ForumThread();
		if (to.getGasUserTo() != null) entity.setGasUserId(to.getGasUserTo().getId());
		entity.setForumMessageList(new ArrayList<ForumMessage>());
		for (ForumMessageTo fmTo:to.getForumMessagesToList()) {
			ForumMessage fm = convert(pm, fmTo, entity);
			fm.setForumThread(entity);
			entity.getForumMessageList().add(fm);
		}
		Gas gas = null;
		if (to.getGasId() != null) gas = pm.getObjectById(Gas.class, to.getGasId());
		entity.setGas(gas);
		//Properties
		entity.setCreationDt(to.getCreationDt());
		entity.setModifiedDt(to.getModifiedDt());
		entity.setLastMessageDt(to.getLastMessageDt());
		entity.setTitle(to.getTitle());
		return entity;
	}
}
